package cn.puge.insurance.service.impl;

import cn.puge.insurance.mapper.PugeInsuranceMapper;
import cn.puge.insurance.mapper.domain.PugeInsurance;
import cn.puge.insurance.s2c.*;
import cn.puge.insurance.service.IInsuranceSVC;
import cn.puge.insurance.utils.ApolloUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: PugeInsuranceSVC
 * @Description: 保险services接口实现层
 * @Author: Gaoruiqi
 * @Date: 2025-11-11 19:59
 * @Version: 1.0
 **/
@Service
@Slf4j
public class InsuranceSVC extends ServiceImpl<PugeInsuranceMapper, PugeInsurance> implements IInsuranceSVC {

    @Resource
    private PugeInsuranceMapper pugeInsuranceMapper;

    @Override
    public List<S2cInsuranceBanner> getInsuranceBannerList() {
        String insuranceCardImgsInfo = ApolloUtil.getPropertiesFromApplication("INSURANCE_CARD_IMGS_INFO", "");
        List<S2cInsuranceBanner> s2CInsuranceBannerList = JSONArray.parseArray(insuranceCardImgsInfo, S2cInsuranceBanner.class);
        return s2CInsuranceBannerList;
    }

    @Override
    public S2cInsurancePageInfo getInsuranceApplication(String insuranceProductNum) {

        S2cInsurancePageInfo s2cInsurancePageInfo = new S2cInsurancePageInfo();
        String insurancePagePlanContent = ApolloUtil.getPropertiesFromApplication("INSURANCE_PAGE_PLAN_CONTENT", "");
        String insurancePagePlanTitle = ApolloUtil.getPropertiesFromApplication("INSURANCE_PAGE_PLAN_TITLE", "");
        String insurancePageTopImage = ApolloUtil.getPropertiesFromApplication("INSURANCE_PAGE_TOP_IMAGE", "");
        List<S2cPlanBzCon> s2cPlanBzCons = JSONArray.parseArray(insurancePagePlanContent, S2cPlanBzCon.class);
        for (S2cPlanBzCon s2cPlanBzCon : s2cPlanBzCons) {
            if (insuranceProductNum.equals(s2cPlanBzCon.getInsuranceProductNum())) {
                List<S2cInsurancePagePlanInfo> plans = s2cPlanBzCon.getPlans();
                s2cInsurancePageInfo.setPlanContent(plans);
            }
        }
        s2cInsurancePageInfo.setPlanTitle(insurancePagePlanTitle);
        s2cInsurancePageInfo.setTopImage(insurancePageTopImage);
        return s2cInsurancePageInfo;
    }

    @Override
    public S2cInsuranceDeal publishInsuranceApplication(PugeInsurance pugeInsurance) {
        pugeInsuranceMapper.insert(pugeInsurance);
        S2cInsuranceDeal s2cInsuranceDeal = new S2cInsuranceDeal();
        BeanUtils.copyProperties(pugeInsurance, s2cInsuranceDeal);
        return s2cInsuranceDeal;
    }
}
